# 将多个从表的多个sheets内容合并到主表中, 不考虑顺序
# 主表可以没有'序号'列, 直接依次将当前slave的行插入到主表末尾
# 需要指定主表数据行第一行的行数, 也就是标题行后一行, 不要求标题行没有合并单元格
# 需要比较主文件和从文件的每一行， 如果主文件空单元格数比从文件的多，则不覆盖

from ExcelClass import Sheet
import pandas

def combine_two_in_one_by_line(path, master_filename, slave_filename, sheet, insert_line_num):
    # inster_line_num为要插入的数据行号，从0开始
    try:
        key_row_num = insert_line_num - 1
        current_row_num = 0
        slave_excel = Sheet(path, slave_filename, sheet, key_row_num)
        master_excel = Sheet(path, master_filename, sheet, key_row_num)
        print(f'{slave_filename}读取成功')
    except:
        print(f'{slave_filename}读取失败')
        # 从文件读取失败, 直接return, 读取下一个从文件
        return
    
    for slave_row_num in range(slave_excel.get_max_row()-1):
        if slave_excel.is_all_nan_by_line(slave_row_num):
            continue
        slave_line_value = slave_excel.get_line(slave_row_num)
        print(f'current row: {current_row_num},data: {slave_line_value}')
        master_excel.cover_line(current_row_num,slave_line_value)
        current_row_num += 1


if __name__ == '__main__':
    file_path = './data_source/'
    master = '发区局土增税-房地产开发项目台账模板202202.xlsx'
    slave1 = '发区局土增税-房地产开发项目台账模板202202--1.xlsx'
    slave2 = '发区局土增税-房地产开发项目台账模板202202--23.xlsx'
    sheetname = 'sheet1'
    # combine_two_in_one_by_order(file_path, master, slave1)
    combine_two_in_one_by_line(file_path, master, slave1, sheetname,4)
